/*********************************************************************************
CREATED BY: SCOTT FIELDER 
CREATED DATE: 4/18/2011


REVISION HISTORY:
-

***********************************************************************************/







global with sharing class FB_Post {

	
	public boolean showThanks {get; set;}
	public string message {get; set;}
	public string link {get; set;}
	public string pictureURL {get; set;}
	public string name {get; set;}
	public string caption {get; set;}
	public string description {get; set;}
	public string actions {get; set;}
	public string privacy {get; set;}
	public string targeting {get; set;}
	public string redirectURL {get; set;}
	public boolean isAuth {get; set;}
	

	public PageReference doFBPost(){
		try{
			Cookie authCookie = ApexPages.currentPage().getCookies().get(FB_Cookie_Name__c.getInstance().Name__c);
			string siteURL = Site.getCurrentSiteUrl();
			
			if(authCookie != null){
				 
				List<string> cookieValList = UTIL_Cookie.doCookieValues(authCookie);
				
				FB_Graph_Settings__c fb = FB_Graph_Settings__c.getInstance();
		  		
		  		FB_Graph cls = new FB_Graph();
				cls.doPOSTCallout(FB_Post.buildFBPost(message, link, pictureURL, 
													name, caption, description, actions, privacy, targeting),
													fb.Graph_Endpoint__c+cookieValList[0]+'/feed?access_token='+
													cookieValList[1]);
			    
			    if(redirectURL != null){
			    	PageReference pgRef = new PageReference(redirectURL);
			    	pgRef.setRedirect(true);
			    	return pgRef;
			    }else{
			    	return null;
			    }
			    
			 }else{
				
				string urlString = 'https://www.facebook.com/dialog/oauth?client_id='+
									FB_Graph_Settings__c.getInstance().FB_App_Id__c+
									'&redirect_uri='+
									siteURL+
									'&scope='+ 
									FB_Graph_Settings__c.getInstance().FB_Permissions_Requested__c;
				
				PageReference pgRef = new PageReference(urlString);
				pgRef.setRedirect(true);
				return pgRef;
				
			}    
			
		}catch(exception e){
			return null;
		}
	
	}

	
	public static string buildFBPost(string message, string link, string pictureURL, 
							  string name, string caption, string description,
							  string actions, string privacy, string targeting){
	
		FB_Graph_Settings__c fb = FB_Graph_Settings__c.getInstance();
		
		string body;
		if(message != null){
			message = EncodingUtil.urlEncode(message, 'UTF-8');
			body = body+ '&message='+message;
		}
		if(link != null){
			link = EncodingUtil.urlEncode(link, 'UTF-8');
			body = body+ '&link='+link;
		}
		if(pictureURL != null){
			body = body+ '&picture='+pictureURL;
		}
		if(name != null){
			name = EncodingUtil.urlEncode(name, 'UTF-8');
			body = body+ '&name='+name;
		}
		if(caption != null){
			caption = EncodingUtil.urlEncode(caption, 'UTF-8');
			body = body+ '&caption='+caption;
		}
		if(description != null){
			description = EncodingUtil.urlEncode(description, 'UTF-8');
			body = body+ '&description='+description;
		}
		
		return body;
	}
	
	
		
	/***********************************************************
	* 						TEST METHODS
	***********************************************************/
	
	static testMethod void test_doFBPost(){
		
		test.startTest(); 
		
		
		FB_Graph_Settings__c fb = FB_Graph_Settings__c.getInstance();
		
		if(fb == null) {
            fb = new FB_Graph_Settings__c(SetupOwnerId = UserInfo.getOrganizationId());
        }
        
        fb.OF__API_Key__c = '111111111111111111111';
        fb.OF__App_Secret__c = '1111111111111111111111111';
        fb.OF__Development_Mode__c = true;
        fb.OF__FB_App_Id__c = '11111111111111111111111';
        fb.OF__FB_Permissions_Requested__c = 'email,read_stream,publish_stream,offline_access,user_birthday';
        fb.OF__Graph_Endpoint__c = 'https://graph.facebook.com/';
        fb.OF__Host_Header__c = 'graph.facebook.com';
        
        Database.upsert(fb);
		
		
		FB_Post cls = new FB_Post();
		
		cls.name = 'test';
		cls.caption = 'test';
		cls.link = 'link';
		cls.description = 'test';
		cls.message = 'message';
		cls.pictureURL = 'pic';
		cls.privacy = 'privacy';
		cls.targeting = 'targeting';
		
		cls.doFBPost();
		
		string uid = '12345';
		string accessToken = '123123123123123123';
		
		FB_Post.buildFBPost(cls.message, cls.link, cls.pictureURL, 
							cls.name, cls.caption, cls.description, 
							cls.actions, cls.privacy, cls.targeting);
		
		test.stopTest();
		
		
	}
	
}